<?php

require_once LIBRARIES_ENTITIES_DATABASE;
require_once LIBRARIES_ENTITIES_CATEGORIES;
require_once LIBRARIES_ENTITIES_ITEMS;
require_once LIBRARIES_TOOLS_XTEMPLATE;
require_once LIBRARIES_TOOLS_UTILITY;

class ComNewItemController {

    public function invoke() {
        $action = '';
        if (isset($_POST['action'])) {
            $action = $_POST['action'];
        }


        // get list of category
        $categories = $this->getCategoryList();

        if (count($categories) > 0) {
            $derectory = dirname(__FILE__);
            $xtemplate = new XTemplate($derectory . DS . 'views' . DS . 'com_default.html');

            // loop each category
            for ($i = 0; $i < count($categories); $i++) {
                $xtemplate->assign('CATEGORY_ID', $categories[$i]->getID());
                $xtemplate->assign('CATEGORY_ALIAS', $categories[$i]->getAlias());
                $xtemplate->assign('CATEGORY_TITLE', $categories[$i]->getTitle());

                $items = $this->getItemListByCategoryID($categories[$i]->getID());

                if (count($items) > 0) {

                    // loop each item
                    for ($y = 0; $y < count($items); $y++) {
                        if ($y < 3) {
                            if ($items[$y]->getState() == 1) {

                                // get days between createdDate and current date
                                $daysBetweenDate = Utility::daysBetweenDate($items[$y]->getCreatedDate(), Utility::getCurrentDate());

                                // if days = 0 and days <= 3 then show new icon
                                if ($daysBetweenDate <= 3) {
                                    $xtemplate->parse('main.category_list.item_list.new_icon');
                                }

                                $xtemplate->assign('ITEM_ID', $items[$y]->getID());
                                $xtemplate->assign('ITEM_ALIAS', $items[$y]->getAlias());
                                $xtemplate->assign('ITEM_IMAGE', $items[$y]->getImage());
                                $xtemplate->assign('ITEM_NAME', $items[$y]->getName());
                                $xtemplate->assign('ITEM_PRICE', Utility::formatToCurrencyString($items[$y]->getPrice()));
                                $xtemplate->parse('main.category_list.item_list');
                            }
                        }
                    }
                    $xtemplate->parse('main.category_list');
                }
            }
            $xtemplate->parse('main');
            $xtemplate->out('main');
        }
    }

    /**
     * description: get list of category
     * @param type $parentID
     * @return type:  list of category object
     */
    private function getCategoryList($parentID = 0) {
        $db = new Database();
        try {
            $category = new Categories();
            $category->setComponentID(1); // mã thành phần sản phẩm
            $category->setParent($parentID);
            $result = $category->getListByParentIDAndComponentID($db);
            $db->submitChanges();
            return $result;
        }
        catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    // get list of item by a category id
    private function getItemListByCategoryID($categoryID) {
        $db = new Database();
        try {
            $item = new Items();
            $item->setCategoryID($categoryID);
            return $item->getListByCategoryID($db);
        }
        catch (PDOException $exc) {
            echo "getItemList error: " . $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

}

?>
